package org.lyd.spring.restful.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.lyd.spring.restful.bean.Employee;
import org.lyd.spring.restful.comom.Result;
import org.lyd.spring.restful.service.EmployeeService;
import org.lyd.spring.restful.vo.req.EmployeeAddVo;
import org.lyd.spring.restful.vo.req.EmployeeUpdateVo;
import org.lyd.spring.restful.vo.resp.EmployeeRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:lyd
 **/
@Tag(name = "员工管理")//描述controller类的作用
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("/api/v1")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/employee/{id}")
    @Parameters({
            @Parameter(name = "id", description = "员工id", required = true)
    })
    @Operation(summary="按照id查询员工信息")
    public Result getEmployee(@PathVariable("id") Integer id) {
        Employee employee = employeeService.get(id);
//        int i=10/0;

        return Result.success(employee);
    }

    @GetMapping("/employees")
    @Operation(summary="查询所有员工信息")
    public Result getAllEmployee() {
        List<Employee> list = employeeService.getList();
        //VO脱敏分成，将敏感数据脱敏
        List<EmployeeRespVo> respVos = list.stream()
                .map(employee -> {
                    EmployeeRespVo employeeRespVo = new EmployeeRespVo();
                    BeanUtils.copyProperties(employee, employeeRespVo);
                    return employeeRespVo;
                }).collect(Collectors.toList());
        return Result.success(respVos);
    }

    /**
     * @param employeeUpdateVo
     * @return
     * @Valid 参数校验 如果校验不通过，目标方法不执行
     */
    @Operation(summary="更新员工信息")
    @PostMapping("/employee")
    public Result updateEmployee(@RequestBody @Valid EmployeeUpdateVo employeeUpdateVo) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeUpdateVo, employee);
        employeeService.updateEmployee(employee);
        return Result.success();
    }

    @PutMapping("/employee")
    @Operation(summary="添加员工信息")
    public Result addEmployee(@RequestBody @Valid EmployeeAddVo employeeAddVo) {
        Employee employee = new Employee();
        //如果没有传入参数，抛出异常MethodArgumentNotValidException，使用全局异常处理
        //属性对拷
        BeanUtils.copyProperties(employeeAddVo,employee);
        employeeService.addEmployee(employee);
        return Result.success();
    }


    @DeleteMapping("/employee/{id}")
    @Operation(summary="删除员工信息")
    public Result deleteEmployee(@PathVariable("id") Integer id) {
        employeeService.deleteEmployee(id);
        return Result.success();
    }

}
